package com.tanhua.admin.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.tanhua.admin.service.AdminService;
import com.tanhua.model.vo.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/system/users")
public class SystemController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @GetMapping("/verification")//管理员登录生成验证码
    public void verification(String uuid, HttpServletResponse response) throws IOException {
        //1、生成验证码图片对象
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(299, 97);
        //2、获取验证码
        String code = lineCaptcha.getCode();
        //3、存入到redis
        redisTemplate.opsForValue().set("CODE:"+uuid,code,3, TimeUnit.MINUTES);
        //4、响应验证码图片（将图片数据写入到响应对象中）
        lineCaptcha.write(response.getOutputStream());
    }

    @PostMapping("/login")      //管理员登录校验身份信息
    public ResponseEntity login(@RequestBody Map map){
        //1.调用service处理业务代码
        Map tokenMap=adminService.login(map);
        //2.返回数据
        return ResponseEntity.ok(tokenMap);
    }

    @PostMapping("/profile")    //管理员信息返回
    public ResponseEntity profile(){
        AdminVo vo=adminService.profile();
        return ResponseEntity.ok(vo);
    }

}
